package com.zdpx.pxframework.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdpx.pxframework.core.validator.group.AddGroup;
import com.zdpx.pxframework.core.validator.group.UpdateGroup;
import com.zdpx.pxframework.core.vo.PageResult;
import com.zdpx.pxframework.core.vo.Result;
import com.zdpx.pxframework.system.api.PostInfoControllerApi;
import com.zdpx.pxframework.system.entity.PostInfo;
import com.zdpx.pxframework.system.service.PostInfoService;
import com.zdpx.pxframework.system.vo.query.PostInfoQuery;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * 岗位 接口控制器
 *
 * @Description:
 * @Author: Cheng XiaoXiao
 * @Date: 2022-02-27 15:20:14
 */
@RestController
@RequestMapping("/post-info")
public class PostInfoController implements PostInfoControllerApi {
    @Resource
    private PostInfoService postInfoService;


    /**
     * 获取条件查询信息
     *
     * @param postInfoQuery 查询条件
     * @return 查询封装
     */
    private LambdaQueryWrapper<PostInfo> getPostInfoQueryWrapper(PostInfoQuery postInfoQuery) {
        LambdaQueryWrapper<PostInfo> wrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(postInfoQuery.getPostName())) {
            wrapper.like(PostInfo::getPostName, postInfoQuery.getPostName());
        }
        if (!StringUtils.isEmpty(postInfoQuery.getCode())) {
            wrapper.like(PostInfo::getCode, postInfoQuery.getCode());
        }
        if (postInfoQuery.getSort() != null) {
            wrapper.eq(PostInfo::getSort, postInfoQuery.getSort());
        }
        if (!StringUtils.isEmpty(postInfoQuery.getRemark())) {
            wrapper.like(PostInfo::getRemark, postInfoQuery.getRemark());
        }
        return wrapper;
    }

    /**
     * 按照条件查询岗位信息
     *
     * @param postInfoQuery 查询条件
     * @return 条件结果信息
     */
    @Override
    @GetMapping
    public Result<List<PostInfo>> list(PostInfoQuery postInfoQuery) {
        return Result.success(postInfoService.list(getPostInfoQueryWrapper(postInfoQuery)));
    }

    /**
     * 分页及条件查询岗位信息
     *
     * @param page          页码
     * @param size          分页大小
     * @param postInfoQuery 查询条件
     * @return 分页结果信息
     */
    @Override
    @GetMapping("/{size}/{page}")
    public Result<PageResult<PostInfo>> search(@PathVariable("size") Long size, @PathVariable("page") Long page, PostInfoQuery postInfoQuery) {
        if (page <= 0) {
            page = 1L;
        }
        if (size <= 0) {
            size = 10L;
        }
        Page<PostInfo> pageParam = new Page<>(page, size);

        postInfoService.page(pageParam, getPostInfoQueryWrapper(postInfoQuery));
        PageResult<PostInfo> pageResult = new PageResult<PostInfo>(pageParam.getTotal(), pageParam.getSize(), pageParam.getCurrent(), pageParam.getRecords());

        return Result.success(pageResult);
    }

    /**
     * 根据ID获取岗位详情
     *
     * @param id 岗位ID
     * @return 岗位详细信息
     */
    @Override
    @GetMapping("/{id}")
    public Result<PostInfo> detail(@PathVariable("id") String id) {
        return Result.success(postInfoService.detailById(id));
    }

    /**
     * 增加岗位信息
     *
     * @param postInfo 岗位增加实体
     * @return 增加后的岗位信息
     */
    @Override
    @PostMapping
    public Result<PostInfo> add(@RequestBody @Validated(AddGroup.class) PostInfo postInfo) {
        postInfoService.savePostInfo(postInfo);
        return Result.success(postInfo);
    }

    /**
     * 修改岗位信息
     *
     * @param id       岗位ID
     * @param postInfo 岗位修改实体
     * @return 修改后的岗位信息
     */
    @Override
    @PutMapping("/{id}")
    public Result<PostInfo> updateById(@PathVariable("id") String id, @RequestBody @Validated(UpdateGroup.class) PostInfo postInfo) {
        postInfo.setId(id);
        postInfoService.updatePostInfoById(postInfo);
        return Result.success(postInfoService.getById(id));
    }


    /**
     * 删除岗位信息
     *
     * @param id 岗位ID
     * @return 删除结果
     */
    @Override
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteById(@PathVariable("id") String id) {
        postInfoService.deleteById(id);
        return Result.success(true);
    }

    /**
     * 删除岗位信息
     *
     * @param idList 岗位ID数组
     * @return 删除结果
     */
    @Override
    @DeleteMapping("/batch-remove")
    public Result<Boolean> batchRemove(@RequestBody List<String> idList) {
        postInfoService.removeByIds(idList);
        return Result.success(true);
    }
}
